home *** CD-ROM | disk | FTP | other *** search
- /*
- File: setSoundSRC.c
-
- Contains: Think C program to set the sound output sample rate
-
- Written by: Gary Anwyl
-
- Description: This program finds the "Built-in" sound driver,
- gets the list of sample rates it supports and
- if it supports 44.1 Khz sets it to that rate.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <console.h>
- #include <Components.h>
- #include <Sound.h>
- #include <SoundInput.h>
-
- // It might be necessary to comment out these definitions depending upon
- // which set of include files are being used.
-
- typedef unsigned long UnsignedFixed; /*unsigned fixed-point number*/
- #undef ComponentCallNow
- #define ComponentCallNow(callNumber, paramSize)
- {0x2f3c, paramSize, callNumber, 0x7000, 0xA82A}
-
- #include "SoundComponents.h"
-
-
- #define kRate22050 (22050L<<16)
- #define kRate24000 (24000L<<16)
- #define kRate44100 (44100L<<16)
-
- typedef struct {
- short count;
- UnsignedFixed **rates;
- } SRCInfoStruct;
-
-
- void main(int argc, char *argv[])
- {
- int i;
- OSErr err;
- Component comp;
- ComponentInstance compInstance;
- ComponentDescription cd, cd1;
- Handle nameHandle;
- SRCInfoStruct srcInfo;
-
- InitGraf(&qd.thePort);
-
- // Allocate a handle for the name
- nameHandle = NewHandle(256);
- if (nameHandle == 0) {
- DebugStr("\pNewHandle failed");
- exit(1);
- }
- HLock(nameHandle);
-
- // Search all 'sdev' components for the "Built-In" sound driver
- cd.componentType = 'sdev';
- cd.componentSubType = 0;
- cd.componentManufacturer = 0;
- cd.componentFlagsMask = 0;
- comp = 0;
-
- while (1) {
- comp = FindNextComponent(comp, &cd);
- if (comp == 0) {
- DebugStr("\pBuilt-In sdev not found");
- goto errExit1;
- }
- err = GetComponentInfo(comp, &cd1, nameHandle, 0 , 0);
- if (err != noErr) {
- DebugStr("\pGetComponentInfo failed");
- goto errExit1;
- }
-
- // nameHandle is a pascal string. See if it is "\pBuilt-in"
- if (strncmp("Built-in", (*nameHandle)+1, (**nameHandle)) == 0)
- break;
- }
-
- // Open the sound component
- compInstance = OpenComponent(comp);
- if (compInstance == 0) {
- DebugStr("\pOpenComponent failed");
- goto errExit1;
- }
-
- // See if 44.1KHz is supported. This returns a range or an array of rates
- err = SoundComponentGetInfo(compInstance, 0, siSampleRateAvailable, &srcInfo);
- if (err != noErr) {
- DebugStr("\pSoundComponentGetInfo failed");
- goto errExit2;
- }
-
- if (srcInfo.count == 0) {
- // The lower and upper bounds of a range was returned.
- if ((*srcInfo.rates)[0] > kRate44100 || (*srcInfo.rates)[1] < kRate44100) {
- DebugStr("\p44100 not supported");
- goto errExit2;
- }
- }
- else {
- // Search array of available rates for 44.1 KHz
- for (i=0; 1; i++) {
- if (i==srcInfo.count) {
- DebugStr("\p44100 not supported");
- goto errExit2;
- }
- if ((*srcInfo.rates)[i] == kRate44100)
- break;
- }
- }
-
- // Set the sample rate
- err = SoundComponentSetInfo(compInstance, 0, siSampleRate, (void *)kRate44100);
- if (err != noErr) {
- DebugStr("\pSoundComponentSetInfo failed");
- goto errExit2;
- }
-
- CloseComponent(compInstance);
- exit(0);
-
- // Error exit points
- errExit2:
- CloseComponent(compInstance);
- errExit1:
- exit(1);
- }
-
-